package com.example.expandablelistviewtest;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpandableListviewAdapter extends BaseExpandableListAdapter {
	private List<String> groupList = new ArrayList<String>();
	private List<String> childList = new ArrayList<String>();
	private LayoutInflater inflater;

	public ExpandableListviewAdapter(Context context) {
		if(inflater == null){
			inflater = LayoutInflater.from(context);
		}
	}

	public void resetGroupList(List<String> list) {
		this.groupList = list;
	}

	public void resetChildList(List<String> list) {
		this.childList = list;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return childList.get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return childList.size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groupList.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return groupList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		GroupViewHolder holder = null; 
		if(convertView==null){
			holder = new GroupViewHolder();
			convertView =  inflater.inflate(R.layout.group_item, null);
			holder.groupText = (TextView) convertView.findViewById(R.id.group_item_text);
			convertView.setTag(holder);
		}else{
			holder = (GroupViewHolder) convertView.getTag();
		}
		holder.groupText.setText(groupList.get(groupPosition));
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ChildViewHolder holder = null; 
		if(convertView==null){
			holder = new ChildViewHolder();
			convertView =  inflater.inflate(R.layout.child_item, null);
			holder.childText = (TextView) convertView.findViewById(R.id.child_item_text);
			convertView.setTag(holder);
		}else{
			holder = (ChildViewHolder) convertView.getTag();
		}
		holder.childText.setText(childList.get(groupPosition));
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	class GroupViewHolder {
		TextView groupText;
	}

	class ChildViewHolder {
		TextView childText;
	}
}
